#include "glparse.h"
#include "glcmd.h"
#include "serialview.h"


PARSE_STRUCT parse_uart;
Serial *serial;
extern Log mlog;

void send_uart_data(uint8_t *buf,uint16_t len)
{
    serial->send((char*)buf,len);
}

void GLParse::getFrame()
{
    emit getFrameSignals(parse_uart);
}

GLParse::GLParse()
{
    //char *version = nb_crypto_board_get_version();
    //printf("%s\r\n",version);

    parse_struct_init(&parse_uart);
    parse_set_rec_callback(&parse_uart, recv_uart_farme);
    parse_set_send_fun(&parse_uart, send_uart_data);

    //connect(this,SIGNAL(getFrameSignals(PARSE_STRUCT)),this,SLOT(getFrameSlot(PARSE_STRUCT)));
}

void GLParse::init(Serial *sserial)
{
    serial = sserial;
}

void GLParse::receive_uart_data(char *receive_buffer, uint16_t receive_len)
{
    parse_data(&parse_uart, (uint8_t *)receive_buffer, receive_len);
}

void GLParse::send_test()
{
    FRAME_STRUCT frame_s;

    frame_s.Version = PROTOCOL_VERSION;
    frame_s.FrameDataLen = 3;
    frame_s.MasterCmd = MASTER_CMD_SYSTEM;
    frame_s.SlaveCmd = SLAVE_CMD_SYSTEM_GET_VERSION_SUCCESS;
    frame_s.frame_data = NULL;
    creat_send_cmd(&frame_s);
}

void GLParse::printf_frame(FRAME_STRUCT *frame_s)
{
//    qDebug() << "Version: " << frame_s->Version;
//    qDebug() << "FrameDataLen: " << frame_s->FrameDataLen;
//    qDebug() << "MasterCmd: " << frame_s->MasterCmd;
//    qDebug() << "SlaveCmd: " << frame_s->SlaveCmd;
}

void GLParse::getFrameSlot(PARSE_STRUCT parse_s)
{
//    QString str;
//    printf_frame(&parse_s.frame_s);
//    if(parse_s.frame_s.MasterCmd == MASTER_CMD_SYSTEM)
//    {
//        if(parse_s.frame_s.SlaveCmd == SLAVE_CMD_SYSTEM_GET_VERSION_SUCCESS)
//        {
//            str.sprintf("Get Version: %d.%d.%d",
//                        parse_s.frame_s.frame_data[0],
//                    parse_s.frame_s.frame_data[1],
//                    parse_s.frame_s.frame_data[2]);
//            //qDebug() << str;
//            mlog.show(str);
//        }
//    }
}

void GLParse::send_system_get_version(void)
{
    FRAME_STRUCT frame_s;

    frame_s.Version = PROTOCOL_VERSION;
    frame_s.FrameDataLen = 0;
    frame_s.MasterCmd = MASTER_CMD_SYSTEM;
    frame_s.SlaveCmd = SLAVE_CMD_SYSTEM_GET_VERSION;
    frame_s.frame_data = NULL;
    frame_s.send_frame_fun = send_uart_data;

    creat_send_cmd(&frame_s);
}

void GLParse::send_flash_begin(unsigned int size)
{
    unsigned char send_buf[4];
    FRAME_STRUCT frame_s;

    send_buf[0] = (unsigned char)size;
    send_buf[1] = (unsigned char)(size>>8);
    send_buf[2] = (unsigned char)(size>>16);
    send_buf[3] = (unsigned char)(size>>24);

    frame_s.Version = PROTOCOL_VERSION;
    frame_s.FrameDataLen = 4;
    frame_s.MasterCmd = MASTER_CMD_FLASH;
    frame_s.SlaveCmd = SLAVE_CMD_FLASH_START;
    frame_s.frame_data = send_buf;
    frame_s.send_frame_fun = send_uart_data;

    creat_send_cmd(&frame_s);
}

void GLParse::send_flash_trans(unsigned char *buffer,unsigned int len)
{
    FRAME_STRUCT frame_s;

    frame_s.Version = PROTOCOL_VERSION;
    frame_s.FrameDataLen = len;
    frame_s.MasterCmd = MASTER_CMD_FLASH;
    frame_s.SlaveCmd = SLAVE_CMD_FLASH_TRANS;
    frame_s.frame_data = buffer;
    frame_s.send_frame_fun = send_uart_data;

    creat_send_cmd(&frame_s);
}

void GLParse::send_flash_crc(int crc)
{
    unsigned char send_buf[4];
    FRAME_STRUCT frame_s;

    send_buf[0] = (unsigned char)crc;
    send_buf[1] = (unsigned char)(crc>>8);
    send_buf[2] = (unsigned char)(crc>>16);
    send_buf[3] = (unsigned char)(crc>>24);

    frame_s.Version = PROTOCOL_VERSION;
    frame_s.FrameDataLen = 4;
    frame_s.MasterCmd = MASTER_CMD_FLASH;
    frame_s.SlaveCmd = SLAVE_CMD_FLASH_CRC;
    frame_s.frame_data = send_buf;
    frame_s.send_frame_fun = send_uart_data;

    creat_send_cmd(&frame_s);
}




